﻿namespace CgSilverlightDynamicGeometry.Utilities
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using M = System.Math;

    public static class Utilities
    {
        public static void Move(
            this Line line,
            double x1,
            double y1,
            double x2,
            double y2,
            CoordinateSystem coordinateSystem)
        {
            var p1 = coordinateSystem.ToPhysical(new Point(x1, y1));
            var p2 = coordinateSystem.ToPhysical(new Point(x2, y2));
            line.X1 = p1.X;
            line.X2 = p2.X;
            line.Y1 = p1.Y;
            line.Y2 = p2.Y;
        }

        public static void MoveTo(this UIElement element, double x, double y)
        {
            Canvas.SetLeft(element, x);
            Canvas.SetTop(element, y);
        }

        public static void MoveTo(this UIElement element, Point position)
        {
            MoveTo(element, position.X, position.Y);
        }

        public static Point SnapToIntegers(this Point p)
        {
            return new Point(M.Ceiling(p.X), M.Ceiling(p.Y));
        }

        public static Point Scale(this Point p, double scaleFactor)
        {
            return new Point(p.X * scaleFactor, p.Y * scaleFactor);
        }

        public static Point Minus(this Point point, double offset)
        {
            return new Point(point.X - offset, point.Y - offset);
        }

        public static bool IsValidValue(this double value)
        {
            return !double.IsNaN(value)
                && !double.IsInfinity(value);
        }

        public static bool IsValidPositiveValue(this double value)
        {
            return value.IsValidValue()
                && value > 0;
        }

        public static bool IsValidNonNegativeValue(this double value)
        {
            return value.IsValidValue()
                && value >= 0;
        }

        public static double Round(this double num, int fractionalDigits)
        {
            return M.Round(num, fractionalDigits);
        }

        public static double RoundToEpsilon(this double point)
        {
            return point.Round(10);
        }

        public static Point RoundToEpsilon(this Point point)
        {
            return new Point(point.X.RoundToEpsilon(), point.Y.RoundToEpsilon());
        }

        public static Point GetCoordinates(this UIElement element)
        {
            return new Point(Canvas.GetLeft(element), Canvas.GetTop(element));
        }

        public static void SetZIndex(this UIElement element, int zIndex)
        {
            Canvas.SetZIndex(element, zIndex);
        }
    }
}
